package com.offcn.controller;

import com.offcn.bean.User;
import com.offcn.service.UserService;
import com.offcn.utils.OAResult;
import com.offcn.utils.PageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/{page}")
    public String forwardPage(@PathVariable("page") String page, Integer uid, Model model){
        model.addAttribute("uid",uid);
        return "user/" +page;
    }

    @Value("${pageSize}")
    private Integer pageSize;

    @RequestMapping("/getAllUsers")
    @ResponseBody
    public PageView<User> getAllUsers(@RequestParam(defaultValue = "") String uname,
                                      @RequestParam(defaultValue = "1") int currentPage){

        return userService.getAllUsers(currentPage,pageSize,uname);
    }
    @RequestMapping("/deleteUser")
    @ResponseBody
    public OAResult deleteUser(int uid){
        return userService.deleteUser(uid);
    }

    @RequestMapping("/getUserDetailById")
    @ResponseBody
    public User getUserDetailById(int uid){
        return userService.getUserDetailById(uid);
    }
}
